package com.yuchen.ishop.web.app.seller;

import com.yuchen.core.MyPager;
import com.yuchen.ishop.entity.Buyer;
import com.yuchen.ishop.entity.GoodsHouse;
import com.yuchen.ishop.service.Order.OrderRestClient;
import com.yuchen.ishop.service.buyer.BuyerRestClient;
import com.yuchen.ishop.service.goods.GoodsHouseRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import com.yuchen.jaxrs.Responses;
import com.yuchen.utils.security.Base64Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping(value = "/my/seller")
public class MyOrderAction extends BaseAction {

    @Autowired
    private OrderRestClient orderRestClient;

    @Autowired
    private GoodsHouseRestClient goodsHouseRestClient;

    @Autowired
    private BuyerRestClient buyerRestClient;

    @RequestMapping(value = "myOrder")
    public String myOrder(Long pageNo, ModelMap modelMap, HttpServletRequest request) {
        modelMap.addAttribute("myMenu", "order");
        pageNo = pageNo == null ? 1l : pageNo;
        Map map = new HashMap(3);
        map.put("pageNo", pageNo);
        CookieUserInfo userInfo = cookie2UserInfo(request);
        if ("seller".equals(userInfo.getRole()))
            map.put("sellerId", userInfo.getId());
        else {
            map.put("sellerId", userInfo.getSellerId());
            map.put("xiaoerId", userInfo.getId());
        }
        MyPager myPager = orderRestClient.findBySeller(map);
        if (myPager != null && myPager.getRecords() != null && myPager.getRecords().size() > 0) {
            List list = myPager.getRecords();
            Map m = null;
            GoodsHouse house = null;
            Buyer buyer = null;
            for(int i=0;i<list.size();i++){
                m = (Map) list.get(i);
                if(m.get("spid")!=null){
                    house = goodsHouseRestClient.get(Long.valueOf(m.get("spid").toString()));
                }
                if(m.get("buyer_id")!=null){
                    buyer =  buyerRestClient.getById(Long.valueOf(m.get("buyer_id").toString()));
                    if(buyer!=null){
                        buyer.setSalt(null);
                        buyer.setPassword(null);
                    }
                }
                m.put("buyer",buyer);
                m.put("house",house);
                m.put("base64OrderNo", Base64Utils.encodeBase64(m.get("order_no").toString()));
            }
        }
        modelMap.addAttribute("pager", myPager);
        return "my/seller/myOrder";
    }


    @RequestMapping(value = "changeOrderPrice",method = RequestMethod.POST)
    @ResponseBody
    public Responses changeOrderPrice(String subOrderNo,Double realPrice) {
        return orderRestClient.changeOrderPrice(subOrderNo,realPrice);
    }

}
